package algorithm.stringAlgorithm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UncommonFromSentences
 * @Description TODO
 * @Author skywingking
 * @Date 2022/1/30 11:45 下午
 **/
public class UncommonFromSentences {
    public String[] uncommonFromSentences(String s1, String s2) {
        Map<String,Integer> freq = new HashMap<>();
        insert(s1,freq);
        insert(s2,freq);
        List<String> ans = new ArrayList<>();
        for(Map.Entry<String,Integer> entry : freq.entrySet()){
            if(entry.getValue() == 1){
                ans.add(entry.getKey());
            }
        }
        return ans.toArray(new String[0]);
    }

    private void insert(String s,Map<String,Integer> freq){
        String[] arr = s.split(" ");
        for(String word:arr){
            freq.put(word,freq.getOrDefault(word,0) + 1);
        }
    }
}